﻿using EasyDDD.AdminModule.Domain.AggregateSysAdmin;
using EasyDDD.AdminModule.Domain.AggregateSysAdminRole;
using EasyDDD.AdminModule.Domain.AggregateSysMenu;
using EasyDDD.Domain.User;
using EasyDDD.Infrastructure.EFCore.Repository;
using Microsoft.EntityFrameworkCore;

namespace EasyDDD.AdminModule.Repository
{
    public class AdminModuleRepositoryDbContext : EFRepositoryContext<AdminModuleRepositoryDbContext>
    {
        public DbSet<SysMenu> SysMenu { get; set; } = default!;
        public DbSet<SysAdmin> SysAdmin { get; set; } = default!;
        public DbSet<SysAdminRole> SysAdminRole { get; set; } = default!;


        //下面用于dataseed 暂时没有更好的办法 或者专门抽出一个类库处理dataseed
        private DbSet<SysAdminRoleMenu> SysAdminRoleMenu { get; set; } = default!;
        private DbSet<EasyDDD.Domain.User.ApplicationRoleClaim> ApplicationRoleClaim { get; set; } = default!;
        private DbSet<EasyDDD.Domain.User.ApplicationUserClaim> ApplicationUserClaim { get; set; } = default!;
        private DbSet<EasyDDD.Domain.User.ApplicationUserLogin> ApplicationUserLogin { get; set; } = default!;
        private DbSet<EasyDDD.Domain.User.ApplicationUserRole> ApplicationUserRole { get; set; } = default!;
        private DbSet<EasyDDD.Domain.User.ApplicationUserToken> ApplicationUserToken { get; set; } = default!;

        public AdminModuleRepositoryDbContext(DbContextOptions<AdminModuleRepositoryDbContext> options, IMediator mediator, IServiceProvider provider)
            : base(options, mediator, provider)
        {
            Database.EnsureCreated();
            //var s = Database.GetPendingMigrations().Any();

            //var res = Database.EnsureCreated();
            //Console.WriteLine("Database.EnsureCreated:", res);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.EnableSensitiveDataLogging();
            //optionsBuilder.LogTo(Console.WriteLine);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //modelBuilder.ApplyConfiguration(new OrderEntityTypeConfiguration());
            base.OnModelCreating(modelBuilder);

            //TODO: 特性，反射处理 或 源生成器
            //modelBuilder.DomainEntity<SysMenu>().Property(e => e.Id).ValueGeneratedOnAdd();
            modelBuilder.Entity<SysMenu>().Property(e => e.Id)
                .ValueGeneratedOnAdd()
                .HasConversion(v => v.Value, v => new(v));

            this.DataSeed(modelBuilder);
        }

        private void DataSeed(ModelBuilder modelBuilder)
        {
            //modelBuilder.Entity<SysMenu>().HasData(new DataSeed.SysMenuSeed().GetDataSeed());
            //modelBuilder.Entity<SysAdminRole>().HasData(new DataSeed.SysAdminRoleSeed().GetDataSeed());
            //modelBuilder.Entity<SysAdmin>().HasData(new DataSeed.SysAdminSeed().GetDataSeed());


            {
                var path = Path.Combine(AppContext.BaseDirectory, "DataSeed/SysMenu.json");
                var json = File.ReadAllText(path);
                var entities = System.Text.Json.JsonSerializer.Deserialize<List<SysMenu>>(json, System.Text.Json.JsonSerializerOptions.Web);
                modelBuilder.Entity<SysMenu>().HasData(entities!);
            }

            {
                var path = Path.Combine(AppContext.BaseDirectory, "DataSeed/ApplicationRole.json");
                var json = File.ReadAllText(path);
                var entities = System.Text.Json.JsonSerializer.Deserialize<List<SysAdminRole>>(json, System.Text.Json.JsonSerializerOptions.Web);
                modelBuilder.Entity<SysAdminRole>().HasData(entities!);
            }
    
            {
                var path = Path.Combine(AppContext.BaseDirectory, "DataSeed/ApplicationUser.json");
                var json = File.ReadAllText(path);
                var entities = System.Text.Json.JsonSerializer.Deserialize<List<SysAdmin>>(json, System.Text.Json.JsonSerializerOptions.Web);
                modelBuilder.Entity<SysAdmin>().HasData(entities!);
            }
 
            {
                var path = Path.Combine(AppContext.BaseDirectory, "DataSeed/SysAdminRoleMenu.json");
                var json = File.ReadAllText(path);
                var entities = System.Text.Json.JsonSerializer.Deserialize<List<SysAdminRoleMenu>>(json, System.Text.Json.JsonSerializerOptions.Web);
                modelBuilder.Entity<SysAdminRoleMenu>().HasData(entities!);
            }

            {
                var path = Path.Combine(AppContext.BaseDirectory, "DataSeed/ApplicationRoleClaim.json");
                var json = File.ReadAllText(path);
                var entities = System.Text.Json.JsonSerializer.Deserialize<List<ApplicationRoleClaim>>(json, System.Text.Json.JsonSerializerOptions.Web);
                modelBuilder.Entity<ApplicationRoleClaim>().HasData(entities!);
            }

            {
                var path = Path.Combine(AppContext.BaseDirectory, "DataSeed/ApplicationUserClaim.json");
                var json = File.ReadAllText(path);
                var entities = System.Text.Json.JsonSerializer.Deserialize<List<ApplicationUserClaim>>(json, System.Text.Json.JsonSerializerOptions.Web);
                modelBuilder.Entity<ApplicationUserClaim>().HasData(entities!);
            }

            {
                var path = Path.Combine(AppContext.BaseDirectory, "DataSeed/ApplicationUserLogin.json");
                var json = File.ReadAllText(path);
                var entities = System.Text.Json.JsonSerializer.Deserialize<List<ApplicationUserLogin>>(json, System.Text.Json.JsonSerializerOptions.Web);
                modelBuilder.Entity<ApplicationUserLogin>().HasData(entities!);
            }

            {
                var path = Path.Combine(AppContext.BaseDirectory, "DataSeed/ApplicationUserRole.json");
                var json = File.ReadAllText(path);
                var entities = System.Text.Json.JsonSerializer.Deserialize<List<ApplicationUserRole>>(json, System.Text.Json.JsonSerializerOptions.Web);
                modelBuilder.Entity<ApplicationUserRole>().HasData(entities!);
            }

            {
                var path = Path.Combine(AppContext.BaseDirectory, "DataSeed/ApplicationUserToken.json");
                var json = File.ReadAllText(path);
                var entities = System.Text.Json.JsonSerializer.Deserialize<List<ApplicationUserToken>>(json, System.Text.Json.JsonSerializerOptions.Web);
                modelBuilder.Entity<ApplicationUserToken>().HasData(entities!);
            }

        }
    }
}
